package CCRMClient.GUI;

import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JButton;

import CCRMClient.ClientStart;
import Common.SERVER_COMMANDS;
//import Common.Entities.Cart;
import Common.Entities.CustomerResponse;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class CartScreen extends JPanel {
	private JLabel lblCart;
	private JLabel lblProductId;
	private JLabel lblProductName;
	private JLabel lblProductBasePrice;
	private JLabel lblQuantity;
	private JTextField textQuantity;
	private JLabel lblCartPrice;
	private JTextField textCartPrice;
	private JButton btnGoBack;
	private JButton btnSubmit;
	private CustomerResponse response;
	private JLabel lblDollar1;
	private JTextField textProdPrice;
	private JLabel lblDollar;
	private String saleDescription;
	public CartScreen(String[] productData, CustomerResponse response, String saleDescription){
		this.response = response;
		this.saleDescription = saleDescription;
		initComponents(productData);
	}
	private void initComponents(String[] productData) {
		setBackground(Color.WHITE);
		setLayout(null);
		
		lblCart = new JLabel("Cart");
		lblCart.setForeground(Color.RED);
		lblCart.setFont(new Font("David", Font.BOLD, 26));
		lblCart.setHorizontalAlignment(SwingConstants.CENTER);
		lblCart.setBounds(200, 11, 66, 27);
		add(lblCart);
		
		lblProductId = new JLabel("Product ID: " + productData[0]);
		lblProductId.setFont(new Font("David", Font.BOLD, 16));
		lblProductId.setBounds(33, 72, 181, 14);
		add(lblProductId);
		
		lblProductName = new JLabel("Product Name: " + productData[1]);
		lblProductName.setFont(new Font("David", Font.BOLD, 16));
		lblProductName.setBounds(33, 97, 218, 14);
		add(lblProductName);
		
		lblProductBasePrice = new JLabel("Product Price: ");
		lblProductBasePrice.setFont(new Font("David", Font.BOLD, 16));
		lblProductBasePrice.setBounds(33, 122, 140, 14);
		add(lblProductBasePrice);
		
		lblQuantity = new JLabel("Quantity: ");
		lblQuantity.setFont(new Font("David", Font.BOLD, 16));
		lblQuantity.setBounds(33, 147, 75, 14);
		add(lblQuantity);
		
		textQuantity = new JTextField();
		textQuantity.addKeyListener(new TextFieldKeyListener());
		textQuantity.setBounds(104, 144, 23, 20);
		add(textQuantity);
		textQuantity.setColumns(10);
		
		lblCartPrice = new JLabel("Cart Price: ");
		lblCartPrice.setFont(new Font("David", Font.BOLD, 16));
		lblCartPrice.setBounds(33, 172, 94, 14);
		add(lblCartPrice);
		
		textCartPrice = new JTextField();
		textCartPrice.setHorizontalAlignment(SwingConstants.CENTER);
		textCartPrice.setText("0");
		textCartPrice.setEditable(false);
		textCartPrice.setBounds(114, 169, 58, 20);
		add(textCartPrice);
		textCartPrice.setColumns(10);
		
		btnGoBack = new JButton("Go Back");
		btnGoBack.addActionListener(new BtnGoBackActionListener());
		btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnGoBack.setBounds(10, 266, 98, 23);
		add(btnGoBack);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new BtnSubmitActionListener());
		btnSubmit.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnSubmit.setBounds(342, 266, 98, 23);
		add(btnSubmit);
		
		lblDollar1 = new JLabel("$");
		lblDollar1.setFont(new Font("David", Font.BOLD, 16));
		lblDollar1.setBounds(182, 172, 46, 14);
		add(lblDollar1);
		
		textProdPrice = new JTextField();
		textProdPrice.setText(productData[2]);
		textProdPrice.setEnabled(false);
		textProdPrice.setBounds(136, 119, 58, 20);
		add(textProdPrice);
		textProdPrice.setColumns(10);
		
		lblDollar = new JLabel("$");
		lblDollar.setFont(new Font("David", Font.BOLD, 16));
		lblDollar.setBounds(200, 122, 46, 14);
		add(lblDollar);
	}
	private class TextFieldKeyListener extends KeyAdapter {
		@Override
		public void keyTyped(KeyEvent arg0) {
			float price = (Float.valueOf(textProdPrice.getText()))*Integer.getInteger(textQuantity.getText());
			switch(saleDescription){
			case "2 in price of 1":
				if((Integer.parseInt(textQuantity.getText()) >= 2) && (Integer.parseInt(textQuantity.getText())%2 == 0))
					price *= 0.5;
				if((Integer.parseInt(textQuantity.getText()) >= 2) && (Integer.parseInt(textQuantity.getText())%2 != 0)){
						price = (float) ((Integer.parseInt(textQuantity.getText()) - 1) * 0.5);
						price += Float.valueOf(textProdPrice.getText());
					}
					break;
			case "50 % discount": 
				price *= 0.5;
				break;
			
			
			}
			textCartPrice.setText(String.valueOf(price));
			
		}
	}
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			try {
				if(textQuantity.getText().isEmpty())
					ClientStart.appWindow.notifyUser("Please add at least 1 quantity to the cart", "Cart", "INFORMATION_MESSAGE");
				else
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_RESPONSE, response);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				ClientStart.appWindow.notifyUser("Lost connection with the server", "Cart", "ERROR_MESSAGE");
			}
		}
	}
	private class BtnSubmitActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			float cartPrice = Float.valueOf(textCartPrice.getText());
			String[] split = lblProductId.getText().split(" ");
			int prodID = Integer.parseInt(split[2]);
		//	Cart cart = new Cart(prodID, Short.valueOf(textQuantity.getText()), cartPrice);
			Object[] data = new Object[2];
			data[0] = response;
//			data[1] = cart;
			try {
				ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_RESPONSE_WITH_CART, data);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				ClientStart.appWindow.notifyUser("Lost connection with the server", "Cart", "ERROR_MESSAGE");
				ClientStart.quitApp();
			}
			
		}
	}
}
